import enum
import random

import pygame


class WalkDir(enum.IntEnum):
    down = 0
    left = 1
    right = 2
    up = 3


class DirWalkByAImage:
    """
    4个方向的行走 下 左 右 上
    """
    image_count = 16
    image_col = 4
    image_width = 49
    image_height = 74
    walk_dir = WalkDir.down

    def __init__(self, path_name: str, image_width: int, image_height: int, is_loop: bool):
        self.path_name = path_name
        self.image_index = 0
        self.action_images = []  # 四个方向的图片数组
        self.image_height = image_height
        self.image_width = image_width
        self.is_loop = is_loop
        self.image = pygame.image.load(self.path_name)
        for i in range(0, self.image_count):
            row = int(i / self.image_col)
            col = i % self.image_col
            if col == 0:
                dir = []
                self.action_images.append(dir)
            rect = (col * self.image_width,
                    row * self.image_height,
                    self.image_width,
                    self.image_height)
            image = self.image.subsurface(rect)
            dir.append(image)

    def get_image(self) -> pygame.Surface:
        """
        获取当前显示图片
        :return:
        """
        current_image = self.action_images[self.walk_dir][self.image_index]
        self.image_index += 1
        if self.image_index >= self.image_col:
            if self.is_loop:
                self.image_index = 0
            else:
                self.image_index = self.image_col - 1
        return current_image

    def set_dir(self, dir: WalkDir):
        """
        设置方向
        :param dir:  方向
        :return:
        """
        self.walk_dir = dir

    def is_end(self) -> bool:
        """
        是否播放完毕
        :return:
        """
        if self.is_loop:
            return False
        else:
            if self.image_index >= self.image_col - 1:
                return True
            else:
                return False


class WalkSoldier(pygame.sprite.Sprite):
    rect = ()
    hp = 100
    pos_x = 100
    pos_y = 300
    image_width = 50
    image_height = 100
    walk_action = None

    def __init__(self, work_path: str, pos_x, pos_y, hp):
        super(WalkSoldier, self).__init__()
        self.walk_action = DirWalkByAImage(work_path,
                                           self.image_width, self.image_height, True)
        self.walk_action.set_dir(WalkDir.down)
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.hp = hp
        self.dir_count = 5
        # 人物的碰撞范围
        self.rect = pygame.Rect(self.pos_x - 5, self.pos_y -15, self.image_width-20, self.image_height-30)

    # 0 下 1 左  2 右 3 上
    def run(self, down_flag, key_list, obstacle_group: pygame.sprite.Group, japanese_group: pygame.sprite.Group):
        if self.dir_count == 0:
            self.dir_count = 5
            key = random.randint(0, 4)
            if key == 0:
                self.walk_action.set_dir(WalkDir.down)
            elif key == 1:
                self.walk_action.set_dir(WalkDir.up)
            elif key == 2:
                self.walk_action.set_dir(WalkDir.left)
            elif key == 3:
                self.walk_action.set_dir(WalkDir.right)
        else:
            self.dir_count -= 1
        off_x = 0
        off_y = 0
        if self.walk_action.walk_dir == WalkDir.down:
            off_y += 10
        elif self.walk_action.walk_dir == WalkDir.up:
            off_y += -10
        elif self.walk_action.walk_dir == WalkDir.left:
            off_x += -10
        elif self.walk_action.walk_dir == WalkDir.right:
            off_x += 10
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        obstacle_list = pygame.sprite.spritecollide(self, obstacle_group, False)
        if len(obstacle_list) > 0:
            self.rect = temp_rect
        else:
            self.pos_x += off_x
            self.pos_y += off_y

    def draw(self, surface: pygame.Surface, x, y):
        """
        绘制
        :param surface:
        :param x:
        :param y:
        :return:
        """
        current_image = self.walk_action.get_image()
        surface.blit(current_image, (self.pos_x - x, self.pos_y - y))
        pygame.draw.rect(surface, pygame.Color(0, 255, 0),
                         (self.pos_x - x, self.pos_y - y,
                          35, 70), 1)

    def draw_for_invisible(self, surface: pygame.Surface, x, y):
        """
        绘制不可见人物（Task1中的副队长）
        :param surface:
        :param x:
        :param y:
        :return:
        """
        # pygame.draw.rect(surface, pygame.Color(0, 255, 0),
        #                  (self.pos_x - x + 5, self.pos_y - y + 5,
        #                   35, 70), 1)